< previous page page_9 next page >

Page 9
IV
Thou shalt preinitialize thy strings, lest ye become corrupted.
API functions that take string parameters typically see them as addresses to locations in memory that contain a string terminated by a null character. Many functions can pass string data back to the calling VB application by loading that memory location with the data. However, the API function has no way of knowing how large a space is actually available in the buffer provided. Some functions allow you to pass the buffer size as a separate parameter. Others simply require that a buffer be of a certain size. In either case, if you pass an uninitialized or empty string to a function, the memory buffer has either no space or a single character (the NULL terminating character). If the API function tries loading data into the memory address provided, it is sure to overwrite critical data in your application memory space. Bugs introduced in this manner can range from immediate memory exceptions to subtle, rare bugs that only appear once you've shipped thousands of copies of your application. Be sure to initialize string buffers if there is any chance that the API function can modify the buffer.
V
Thou shalt not use as any, for it is evil.
When you declare a parameter type As Any, you are placing the full responsibility for passing the correct data type to the code where the function is called. Visual Basic does no type checking whatsoever. The most common use of As Any declarations is where API function parameters can take more than one data type. Fortunately, Visual Basic allows you to create multiple declarations for the same function, thanks to the Alias option in the Declare statement.
But there are cases where it's just far more convenient to leave a parameter As Any. Sometimes a function can take too many different data types, and it would be inconvenient to define and remember a different declaration for each one. Or perhaps the function has more than one parameter that can take multiple data types, in which case each new type requires multiple declarations.
Whichever approach you choose, it's important to understand As Any parameters and how they work. Besides, you never know when you'll run into some code written by someone who doesn't follow these commandments.
VI
Thou shalt specify option explicit.
The Require Variable Declarations editor option is one that you should always set when working with Visual Basic. If you are reworking code that was created without this option set, be sure to add the command Option Explicit to the start of each code module (including forms, UserControls, classes, and so forth).
Without this option set, any reference to a variable automatically creates an empty instance of that variable. This includes cases where you accidentally create a new variable by misspelling the name of an existing variable. Worse yet, from the perspective of an API programmer, the newly created variable will

 
< previous page page_9 next page >